<html>
<head><meta charset="utf-8"><title>macro-rules expansion not working? · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro-rules.20expansion.20not.20working.3F.html">macro-rules expansion not working?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="227665949"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro-rules%20expansion%20not%20working%3F/near/227665949" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Roland Kuhn <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro-rules.20expansion.20not.20working.3F.html#227665949">(Feb 24 2021 at 20:25)</a>:</h4>
<p>see <a href="https://github.com/NicolasLagaillardie/mpst_rust_github/blob/e557ea3c2b1ccbf98d76b9aa2e2320468ac00da2/examples/rk.rs#L89-L115">this macro</a> for an example: While for <code>println!</code> I get <code>()</code> as return type, the example macro yields <code>{unknown}</code> in rust-analyzer (in that repository there are tons of macros, and I couldn’t find one that was successfully expanded to yield type hints). Perhaps noteworthy is that the recursive expansion preview in VS Code shows only one level of expansion. It would be so super-nice if this would work because session types (what I’m working on) have some corner cases that just require some boilerplate, and I’d love to get rid of it.</p>



<a name="227667234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro-rules%20expansion%20not%20working%3F/near/227667234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro-rules.20expansion.20not.20working.3F.html#227667234">(Feb 24 2021 at 20:33)</a>:</h4>
<p>We don't support macros in type position at the moment</p>



<a name="227669808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro-rules%20expansion%20not%20working%3F/near/227669808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Roland Kuhn <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro-rules.20expansion.20not.20working.3F.html#227669808">(Feb 24 2021 at 20:48)</a>:</h4>
<p>Hmm, but the expansion also stops for the value part:</p>
<div class="codehilite"><pre><span></span><code>{
  struct Rec {
    pub rec:rec!(@@1 Tx[RoleA,Rx[RoleB,rec]]),
  }fn new() -&gt; Rec {
    Rec {
      rec:rec!(@@2 Tx[RoleA,Rx[RoleB,rec]]),
    }
  }new().rec
}
</code></pre></div>



<a name="227670332"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro-rules%20expansion%20not%20working%3F/near/227670332" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Roland Kuhn <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro-rules.20expansion.20not.20working.3F.html#227670332">(Feb 24 2021 at 20:51)</a>:</h4>
<p>In any case: how much is missing regarding macros in type position? Anything I could help with?</p>



<a name="227673280"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro-rules%20expansion%20not%20working%3F/near/227673280" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro-rules.20expansion.20not.20working.3F.html#227673280">(Feb 24 2021 at 21:10)</a>:</h4>
<p>I'm not totally sure, but I think the first step is to duplicate <code>TypeRef</code> into the <code>item_tree</code> module and add a <code>Macro</code> variant to it. These then need to be lowered to the HIR-view <code>TypeRef</code>, at which point the macro needs to be expanded.</p>



<a name="227678462"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro-rules%20expansion%20not%20working%3F/near/227678462" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Roland Kuhn <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro-rules.20expansion.20not.20working.3F.html#227678462">(Feb 24 2021 at 21:46)</a>:</h4>
<p>I managed to simplify it even more, not using macros in type position, and I still get <code>{unknown}</code>:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">RoleA</span><span class="p">;</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">RoleB</span><span class="p">;</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">Tx</span><span class="o">&lt;</span><span class="n">Role</span><span class="p">,</span><span class="w"> </span><span class="n">Cont</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">cont</span>: <span class="nc">Cont</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">_ph</span>: <span class="nc">PhantomData</span><span class="o">&lt;</span><span class="n">Role</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">Role</span><span class="p">,</span><span class="w"> </span><span class="n">Cont</span>: <span class="nb">Default</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Default</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Tx</span><span class="o">&lt;</span><span class="n">Role</span><span class="p">,</span><span class="w"> </span><span class="n">Cont</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">default</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="bp">Self</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">cont</span>: <span class="nc">Cont</span>::<span class="n">default</span><span class="p">(),</span><span class="w"> </span><span class="n">_ph</span>: <span class="nc">PhantomData</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">Rx</span><span class="o">&lt;</span><span class="n">Role</span><span class="p">,</span><span class="w"> </span><span class="n">Cont</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">cont</span>: <span class="nc">Cont</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">_ph</span>: <span class="nc">PhantomData</span><span class="o">&lt;</span><span class="n">Role</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">Role</span><span class="p">,</span><span class="w"> </span><span class="n">Cont</span>: <span class="nb">Default</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Default</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Rx</span><span class="o">&lt;</span><span class="n">Role</span><span class="p">,</span><span class="w"> </span><span class="n">Cont</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">default</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="bp">Self</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">cont</span>: <span class="nc">Cont</span>::<span class="n">default</span><span class="p">(),</span><span class="w"> </span><span class="n">_ph</span>: <span class="nc">PhantomData</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="fm">macro_rules!</span><span class="w"> </span><span class="n">rec</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="cp">$x</span>:<span class="nc">ident</span><span class="p">,</span><span class="w"> </span><span class="cp">$t</span>:<span class="nc">ty</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{{</span><span class="w"></span>
<span class="w">        </span><span class="cp">#[derive(Default)]</span><span class="w"></span>
<span class="w">        </span><span class="k">struct</span> <span class="cp">$x</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="k">impl</span><span class="w"> </span><span class="cp">$x</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">rec</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="cp">$t</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="nb">Default</span>::<span class="n">default</span><span class="p">()</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="cp">$x</span>::<span class="n">default</span><span class="p">().</span><span class="n">rec</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">}};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">rec_test</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">rec</span><span class="o">!</span><span class="p">(</span><span class="n">X</span><span class="p">,</span><span class="w"> </span><span class="n">Tx</span><span class="o">&lt;</span><span class="n">RoleA</span><span class="p">,</span><span class="w"> </span><span class="n">Rx</span><span class="o">&lt;</span><span class="n">RoleB</span><span class="p">,</span><span class="w"> </span><span class="n">X</span><span class="o">&gt;&gt;</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="227680244"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro-rules%20expansion%20not%20working%3F/near/227680244" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edwin Cheng <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro-rules.20expansion.20not.20working.3F.html#227680244">(Feb 24 2021 at 21:59)</a>:</h4>
<p>Your case above is not related to macro: </p>
<p>Simplified case :</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">struct</span> <span class="nc">X</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="k">impl</span><span class="w"> </span><span class="n">X</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">u32</span> <span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="mi">0</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="n">X</span><span class="w"> </span><span class="p">{}.</span><span class="n">foo</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="227688789"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro-rules%20expansion%20not%20working%3F/near/227688789" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro-rules.20expansion.20not.20working.3F.html#227688789">(Feb 24 2021 at 22:52)</a>:</h4>
<p>Ah, yes, we don't support <code>impl</code>s inside blocks either</p>



<a name="227688818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro-rules%20expansion%20not%20working%3F/near/227688818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro-rules.20expansion.20not.20working.3F.html#227688818">(Feb 24 2021 at 22:52)</a>:</h4>
<p>This is <a href="https://github.com/rust-analyzer/rust-analyzer/issues/7633">https://github.com/rust-analyzer/rust-analyzer/issues/7633</a></p>



<a name="227731029"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/macro-rules%20expansion%20not%20working%3F/near/227731029" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Roland Kuhn <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/macro-rules.20expansion.20not.20working.3F.html#227731029">(Feb 25 2021 at 08:00)</a>:</h4>
<p>Thanks, with this I was able to sidestep the issue by just putting the declaration on the module level!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>